package hongcaosp.app.android.login;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;

import hongcaosp.app.android.modle.bean.UserInfo;
import hongcaosp.app.android.redpacket.RedPacketService;
import xlj.lib.android.base.component.ActivityStack;
import xlj.lib.android.base.utils.JsonUtil;

public class UserToken {
    private UserInfo user;
    private Context context;

    private List<UserChangeListener> changeListeners = new ArrayList<>();

    private static UserToken defaultUser;

    private UserToken() {

    }

    public static UserToken getDefault() {
        if(defaultUser == null) {
            defaultUser = new UserToken();
        }
        return defaultUser;
    }

    public void register(UserChangeListener userChangeListener) {
        changeListeners.add(userChangeListener);
    }

    public void unregister(UserChangeListener userChangeListener) {
        changeListeners.remove(userChangeListener);
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public boolean isCurrentUser(long userId) {
        if (getUser() != null) {
            return getUser().getId() == userId;
        }
        return false;
    }

    public void setUser(UserInfo user) {
        this.user = user;
        save();
        for (UserChangeListener uc:
                changeListeners) {
            uc.onUserChange(user);
        }
        Activity top = ActivityStack.topActivity();
        if (top != null) {
            Intent intent = new Intent(top, RedPacketService.class);
            intent.putExtra("msgType", RedPacketService.MSGTYPE_START);
            top.startService(intent);
        }
    }

    public void updateUserInfo(UserInfo user) {
        this.user.setVipExpiration(user.getVipExpiration());
        this.user.setVip(user.isVip());
        this.user.setAge(user.getAge());
        this.user.setBirthday(user.getBirthday());
        this.user.setCollectionCounts(user.getCollectionCounts());
        this.user.setDynamicCount(user.getDynamicCount());
        this.user.setFansCounts(user.getFansCounts());
        this.user.setFollowCounts(user.getFollowCounts());
        this.user.setImage(user.getImage());
        this.user.setLocality(user.getLocality());
        this.user.setInvitationCode(user.getInvitationCode());
        this.user.setLikeVideoCount(user.getLikeVideoCount());
        this.user.setMeFollowmTa(user.meFollowTa());
        this.user.setMobile(user.getMobile());
        this.user.setNickName(user.getNickName());
        this.user.setSchoolName(user.getSchoolName());
        this.user.setSex(user.getSex());
        save();
    }

    public UserInfo getUser() {
        if (user == null) {
            SharedPreferences sp =  context.getSharedPreferences("user_information_sp",Context.MODE_PRIVATE);
            if (sp.contains("userinfo")) {
                user = JsonUtil.parse(sp.getString("userinfo",null),UserInfo.class);
            }
        }
        return user;
    }

    public String getToken() {
        if (getUser() != null) {
            return user.getToken();
        }
        return null;
    }

    public void save() {
        if (context == null) return;
        SharedPreferences sp =  context.getSharedPreferences("user_information_sp",Context.MODE_PRIVATE);
        if (user != null) {
            sp.edit().putString("userinfo",JsonUtil.toJsonStr(user)).apply();
        } else {
            sp.edit().remove("userinfo").apply();
        }
    }

    public void clearToken() {
        user = null;
        save();
        for (UserChangeListener uc:
             changeListeners) {
            uc.onUserChange(user);
        }

        Activity top = ActivityStack.topActivity();
        if (top != null) {
            Intent intent = new Intent(top, RedPacketService.class);
            intent.putExtra("msgType", RedPacketService.MSGTYPE_STOP);
            top.startService(intent);
        }
    }

    public boolean isLogged() {
        if (TextUtils.isEmpty(getToken())) {

            return false;
        }
        return true;
    }

    public boolean isLogged(Context context) {
        if (!isLogged()) {
            RegisterActivity.toRegister(context);
            return false;
        }
        return true;
    }

    public interface UserChangeListener {
        void onUserChange(UserInfo user);
    }
}
